package com.itheima.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author lifeihu
 * @date 2021/11/21
 * @TIME:19:04
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest req, @RequestBody Employee employee){
        String password = employee.getPassword();
        //MD5加密
        password= DigestUtils.md5DigestAsHex(password.getBytes());
        String username = employee.getUsername();
        LambdaQueryWrapper<Employee> wapper=new LambdaQueryWrapper<>();
        wapper.eq(Employee::getUsername,username);
        Employee one = employeeService.getOne(wapper);
        if(one==null){
            return R.error("用户不存在");
        }
        if(!password.equals(one.getPassword())){
            return R.error("密码不正确");
        }
        if(one.getStatus()==0){
            return R.error("用户已禁用");
        }
        //id存入session
        HttpSession session = req.getSession();
        session.setAttribute("employee",one.getId());
        return R.success(one);
    }
    @PostMapping("/logout")
    public R logout(HttpServletRequest req){
        HttpSession session = req.getSession();
        session.setAttribute("employee",null);
        return R.success("退出成功");
    }
    @PostMapping
    public R add(HttpServletRequest req ,@RequestBody Employee employee){
        log.info("新增员工，员工信息：{}",employee.toString());

        //设置初始密码123456，需要进行md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());

        //获得当前登录用户的id
//        Long empId = (Long) req.getSession().getAttribute("employee");
//        BaseContext.setCurrentId(empId);

//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);

        employeeService.save(employee);
        return R.success("新增员工成功");
    }
    @GetMapping("/page")
    public R findByPage(Integer page,Integer pageSize,String name){
        IPage<Employee> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> wapper=new LambdaQueryWrapper<>();
        if(name!=null) {
            wapper.like(/*name!=null,*/Employee::getName, name);
        }
        IPage<Employee> page1 = employeeService.page(pageInfo, wapper);
        return R.success(page1);
    }
    @PutMapping()
    public R update(HttpServletRequest req ,@RequestBody Employee employee){
        if(employee.getId()==1l){
            return R.success("该用户不能被禁用");
        }
        Employee emp = employeeService.getById(employee.getId());
        if(emp==null){
            return R.error("用户不存在");
        }
        HttpSession session = req.getSession();
        Long empId = (Long) session.getAttribute("employee");
//        employee.setStatus(employee.getStatus());
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empId);
//        BaseContext.setCurrentId(empId);
        log.info("只能管理员或者用户自己修改自己个人信息...");
        if(employee.getId().longValue()!=empId.longValue()&&empId.longValue()!=1l){
        return R.error("权限不足");
        }
        employeeService.updateById(employee);
        return R.success("修改成功");
    }
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        log.info("根据id查询员工信息...");
        Employee employee = employeeService.getById(id);
        if(employee != null){
            return R.success(employee);
        }
        return R.error("没有查询到对应员工信息");
    }
}
